'''
#Coding: utf-8
#Project: online-exam
#Author: rtf
#Time: 2022-07-27 18:39:10
#FileName: student.py
#Softwar: PyCharm
'''


from trump.query import get_item, get_items, get_table_header, modify_item
from trump.utils import ok, fail
from trump.decorators import table_headers
from utils.params_validate_decorator import validate_params
from funcs.user_info import get_uesr_info


@validate_params([["student_no", str, True]])
async def post(app, request):
    params = request.json
    student = await get_item(app.ctx.pool, "student", params.get("student_no"), column="student_no")
    if student:
        return fail(f'{params.get("student_no")}已存在！')
    params["password"] = app.ctx.fernet.encrypt("123456")


async def put(app, request, id):
    pass


async def ls(app, request):
    pass


async def post_ls(app, request, data):
    college = await get_items(app.ctx.pool, "college")
    specialty = await get_items(app.ctx.pool, "specialty")
    for i in data:
        for x in college:
            if i.get("college_code") == x.get("code"):
                i["college_name"] = x.get("name")
        for x in specialty:
            if i.get("specialty_code") == x.get("code"):
                i["specialty_name"] = x.get("name")
        del i["password"]


async def get(app, request, id):
    pass


async def post_get(app, request, data, id):
    college = await get_item(app.ctx.pool, "college", data.get("college_code"), column="code")
    specialty = await get_item(app.ctx.pool, "specialty", data.get("specialty_code"), column="code")
    del data["password"]
    data["college_name"] = college.get("name") if college else None
    data["specialty_name"] = specialty.get("name") if college else None


async def delete(app, request, id):
    pass